<?php if ( ! defined( 'BASEPATH' ) ) exit( 'No direct script access allowed' );
/**
 * BackendPro
 *
 * A website backend system for developers for PHP 4.3.2 or newer
 *
 * @package         conlabz CMS
 * @author          Christian Schneider
 * @copyright       Copyright (c) 2009
 * @license         
 * @link            http://www.conlabz.de
 * @filesource
 */

// ---------------------------------------------------------------------------

/**
 * Blog_model
 *
 * Model used to retrive webites
 *
 * @package			conlabz CMS
 * @subpackage		Models
 */
class Orgunit_model extends Base_model {
	function Orgunit_model( ) {
		parent::Base_model( );

		$this->_TABLES = array( 'Option' => $this->config->item( 'backendpro_table_prefix' ) . 'article' );

		// Cache to store already fetched items
		$this->_CACHE = array( );

		$this->load->module_model( 'article', 'category_model' );
		
		// Object keyword
		// I wouldn't advise changing this, it could corrupt current
		// articles, the reason for needing this is to stop unserialze
		// errors spamming the log files
		$this->object_keyword = "BeP::Object::";

		log_message( 'debug', 'conlabz CMS : OrgUnit_model class loaded' );
	}

	/**
	 * Get Blogs
	 *
	 * @access public
	 * @param mixed $where Where query string/array
	 * @param array $limit Limit array including offset and limit values
	 * @return object
	 */
	function getOrgUnits( $where = NULL, $limit = array( 'limit' => NULL, 'offset' => '' ) ) {
		$this->db->select( 'orgunit.id, 
							orgunit.name' 
						 );
		$this->db->from( "orgunit" );
		$this->db->from( "category" );
		$this->db->where( "`orgunit`.`category_id` = `category`.`id` AND `orgunit`.`category_language_id` = `category`.`language_id` AND `category`.`website_id` = " . $this->session->userdata['website_id'] );
		if( !is_null( $where ) ) {
			$this->db->where( $where );
		}
		if( !is_null( $limit['limit'] ) ) {
			$this->db->limit( $limit['limit'], ( isset( $limit['offset'] ) ? $limit['offset'] : '' ) );
		}
		$this->db->order_by('orgunit.name desc'); 
		return $this->db->get( );
	}
	
	/**
	 * Insert new Blog
	 *
	 * @access public
	 * @param array $data Data array
	 */
	function insert( $data = array( ) ) {
		//create new orgunit entry
		$this->db->set( 'id',          $data['id']          );
		$this->db->set( 'name',        $data['name']        );
		$this->db->set( 'email',       $data['email']      );
		$this->db->set( 'description', $data['description'] );
		$this->db->set( 'category_id', $data['category_id'] );
		//get category_language_id by category_id
		$this->db->set( 'category_language_id', $this->category_model->getCategoryLanguageId( $data['category_id'] ) );
		
		//insert the new blog in the blog DB
		$this->db->insert( 'orgunit' );
		
		return $this->db->insert_id( );
	}
	
	/**
	 * Update Blog
	 *
	 * @access public
	 * @param array $data Data array
	 */
	function update( $data = array( ), $id ) {		
		$orgunit = array( );
		$orgunit['name']        = $data['name'];
		$orgunit['email']       = $data['email'];
		$orgunit['description'] = $data['description'];
		$orgunit['category_id'] = $data['category_id'];
		//get category_language_id by category_id
		$orgunit['category_language_id'] = $this->category_model->getCategoryLanguageId( $data['category_id'] );
		//insert the new blog
		$this->db->update( 'orgunit', $orgunit, array( 'id' => $id ) );
	}
	
	/**
	 * Constructs an array of the given blogs to be used for a dropdown menu
	 *
	 * @access public
	 * @return array
	 */
	function buildOrgUnitDropdown( ) {
		$orgunits = $this->getOrgUnits( );
		
		$dropdown = array( );
		foreach( $orgunits->result( ) as $row ) {
	    	 $dropdown[$row->id] = $row->name;
		}
		
		return $dropdown;
	}
}
/* End of file orgunit_model.php */
/* Location: ./modules/people/models/orgunit_model.php */
